<!--
 * @Description: 绑定设备页面
 * @Author: Ronda
 * @Date: 2022-02-16 09:30:08
 * @LastEditors: Ronda
 * @LastEditTime: 2022-05-23 15:08:33
-->
<template>
  <div>
    <!-- 页面头部 -->
    <el-page-header @back="$router.go(-1)" content="工程详情"> </el-page-header>
    <!-- 工程信息 -->
    <el-row
      style="background-color: #f4f4f4; border-radius: 4px; margin-top: 5px"
    >
      <el-col :span="24" style="margin: 12px 0px 12px 15px"
        ><div class="grid-content bg-purple-dark">工程信息</div></el-col
      >
    </el-row>
    <div class="content1">
      <el-row>
        <el-col :span="6"
          ><div class="grid-content bg-purple">工程编号</div></el-col
        >
        <el-col :span="6"
          ><div class="grid-content bg-purple-light">
            {{ engineerData.serial_number }}
          </div></el-col
        >
        <el-col :span="6"
          ><div class="grid-content bg-purple">工程名称</div></el-col
        >
        <el-col :span="6"
          ><div class="grid-content bg-purple-light">
            {{ engineerData.name }}
          </div></el-col
        >
      </el-row>
      <el-row>
        <el-col :span="6"
          ><div class="grid-content bg-purple">工程类型</div></el-col
        >
        <el-col :span="6"
          ><div class="grid-content bg-purple-light">
            {{ engineerData.type }}
          </div></el-col
        >
        <el-col :span="6"
          ><div class="grid-content bg-purple">工程状态</div></el-col
        >
        <el-col :span="6"
          ><div class="grid-content bg-purple-light">
            <el-tag type="success" v-if="engineerData.status === '监测中'"
              >监测中</el-tag
            >
            <el-tag v-else-if="engineerData.status === '未绑定'" type="warning"
              >未绑定</el-tag
            >
            <el-tag v-else type="danger">已结束</el-tag>
          </div></el-col
        >
      </el-row>
      <el-row>
        <el-col :span="6"
          ><div class="grid-content bg-purple">开始时间</div></el-col
        >
        <el-col :span="6"
          ><div class="grid-content bg-purple-light">
            {{ engineerData.begin_time }}
          </div></el-col
        >
        <el-col :span="6"
          ><div class="grid-content bg-purple">结束时间</div></el-col
        >
        <el-col :span="6"
          ><div class="grid-content bg-purple-light">
            {{ engineerData.end_time }}
          </div></el-col
        >
      </el-row>
      <el-row>
        <el-col :span="12"
          ><div class="grid-content bg-purple">地址</div></el-col
        >
        <el-col :span="12"
          ><div class="grid-content bg-purple-light">
            {{ engineerData.address }}
          </div></el-col
        >
      </el-row>
      <el-row>
        <el-col :span="12"
          ><div class="grid-content bg-purple">工程负责人</div></el-col
        >
        <el-col :span="12"
          ><div class="grid-content bg-purple-light">
            {{ engineerData.charge_name }}
          </div></el-col
        >
      </el-row>
    </div>
    <!-- 绑定信息 -->
    <div class="binding">
      <el-row
        style="background-color: #f4f4f4; border-radius: 4px; margin-top: 15px"
        type="flex"
        justify=""
      >
        <el-col :span="20" style="margin: 12px 0px 12px 15px"
          ><div class="grid-content bg-purple-dark">绑定信息</div></el-col
        >
        <el-col :span="4" style="text-align: right">
          <el-button type="primary" @click="toBind" style="margin-top: 5px"
            >绑定</el-button
          >
        </el-col>
      </el-row>

      <!-- 绑定信息表格 -->
      <template>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column type="index" :index="1" label="序号" width="180">
          </el-table-column>
          <el-table-column prop="serial_number" label="设备编号" width="180">
          </el-table-column>
          <el-table-column prop="name" label="设备名称"> </el-table-column>
          <el-table-column prop="latitude" label="经度"> </el-table-column>
          <el-table-column prop="longitude" label="纬度"> </el-table-column>
          <el-table-column prop="video" label="视频地址"> </el-table-column>
          <el-table-column prop="online_status" label="在线状态">
            <template v-slot="scope">
              <div>
                <el-tag type="success" v-if="scope.row.online_status === 1"
                  >在线</el-tag
                >
                <el-tag type="danger" v-else>离线</el-tag>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <div>
                <el-button type="text" @click="toUnbind(scope.row)"
                  >解绑</el-button
                >
                <el-button type="text" @click="toCloseDevice(scope.row)"
                  >下线</el-button
                >
              </div>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </div>
    <!-- 绑定按钮的抽屉事件 -->
    <el-drawer
      @close="toClose"
      size="50%"
      title="绑定设备"
      :before-close="handleClose"
      :visible.sync="dialog"
      direction="rtl"
      custom-class="demo-drawer"
      ref="drawer"
    >
      <div class="demo-drawer__content">
        <!-- 表单 -->
        <el-form :model="form" :rules="rules" ref="myForm">
          <el-row
            style="
              background-color: #f4f4f4;
              border-radius: 4px;
              margin-top: 5px;
            "
          >
            <el-col :span="24" style="margin: 12px 0px 12px 15px">
              <div class="grid-content bg-purple-dark">1.选择设备</div>
            </el-col>
          </el-row>
          <el-row style="margin-top: 10px">
            <el-col :span="12">
              <el-form-item
                prop="device_id"
                label="设备"
                :label-width="formLabelWidth"
              >
                <el-select
                  v-model="form.device_id"
                  placeholder="请选择要绑定的设备"
                  clearable
                >
                  <el-option
                    v-for="item in deviceData"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12"></el-col>
          </el-row>
          <el-row
            style="
              background-color: #f4f4f4;
              border-radius: 4px;
              margin-top: 5px;
            "
          >
            <el-col :span="24" style="margin: 12px 0px 12px 15px">
              <div class="grid-content bg-purple-dark">2.配置设备</div>
            </el-col>
          </el-row>
          <el-row style="margin-top: 10px">
            <el-col :span="12">
              <el-form-item
                label="经度"
                prop="latitude"
                :label-width="formLabelWidth"
              >
                <el-input v-model="form.latitude" autocomplete="off"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                label="纬度"
                prop="longitude"
                :label-width="formLabelWidth"
              >
                <el-input
                  v-model="form.longitude"
                  autocomplete="off"
                ></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="24">
              <el-form-item
                label="视频流"
                prop="video"
                :label-width="formLabelWidth"
              >
                <el-input v-model="form.video" autocomplete="off"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div class="demo-drawer__footer">
          <el-button @click="dialog = false">取消</el-button>
          <el-button type="primary" @click="toSave">确定</el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
// 导入get请求
import { get, postJSON } from "@/utils/request";
export default {
  data() {
    return {
      // 工程的详情数据
      engineerData: {},
      // 设备详细数据
      tableData: [],
      // 表单对象
      form: {},
      // 抽屉显示和隐藏控制
      dialog: false,
      // 表单宽度设置
      formLabelWidth: "80px",
      // 设备数据
      deviceData: [],
      // 表单校验规则
      rules: {
        device_id: [
          { required: true, message: "请选择设备", trigger: "change" },
        ],
        latitude: [{ required: true, message: "请输入经度", trigger: "blur" }],
        longitude: [{ required: true, message: "请输入纬度", trigger: "blur" }],
        video: [
          { required: true, message: "请输入设备视频地址", trigger: "blur" },
        ],
      },
    };
  },
  computed: {},
  methods: {
    // 关闭模态框，清空表单校验
    toClose() {
      this.$refs["myForm"].resetFields();
      this.dialog = false;
    },
    // 去提交
    toSave() {
      // ref 一个对象，属性是myForm 可以用.访问也可以用[]访问
      this.$refs["myForm"].validate(async (valid) => {
        // 校验通过
        if (valid) {
          // alert('submit!');
          // console.log(this.form);
          // 深拷贝
          let obj = { ...this.form };
          obj.engineer_id = this.engineerData.id;
          // console.log(obj);
          // 发送绑定请求
          let res = await get("/engineer/bindService", obj);
          if (res.status === 200) {
            this.$notify({
              title: "成功",
              message: res.message,
              type: "success",
            });
            // 清空表单校验
            this.toClose();
            // 更新数据
            this.getDeviceData();
          } else {
            this.$notify({
              title: "失败",
              message: res.message,
              type: "error",
            });
          }
          // 关闭抽屉
          this.dialog = false;
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 下线操作
    async toCloseDevice(row) {
      this.$confirm("设备下线后将无法采集数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          // 发送删除请求
          let res = await get("/device/closeDevice", {
            id: row.id,
          });
          console.log(res);
          this.$message({
            type: "success",
            message: "下线成功!",
          });
          // 将页码改为1

          // 重新请求工程数据
          this.getDeviceData();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消下线",
          });
        });
    },
    // 解绑信息函数
    async toUnbind(row) {
      this.$confirm("此操作将解绑该设备, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          // 发送删除请求
          let res = await get("/engineer/unbindDevice", {
            engineer_id: this.engineerData.id,
            device_id: row.id,
          });
          // console.log(res);
          this.$message({
            type: "success",
            message: "解绑成功!",
          });
          // 将页码改为1

          // 重新请求工程数据
          this.getDeviceData();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消解绑",
          });
        });
    },
    // 绑定信息函数
    toBind() {
      this.getFreeDevice();
      // 弹出对话框
      this.dialog = true;
      // 修改标题
      this.title = "绑定设备";
    },
    // 获取工程对应设备信息函数
    async getDeviceData() {
      let res = await get("/device/pageQuery", {
        page: 1,
        pageSize: 10,
        engineer_id: this.engineerData.id,
      });
      // console.log(this.engineerData,'data');
      // console.log(res);
      this.tableData = res.data.list;
    },
    // 获取空闲设备信息函数
    async getFreeDevice() {
      // 清空空闲设备信息
      this.deviceData=[];
      let res = await get("/device/pageQuery", {
        page: 1,
        pageSize: 10,
      });
      // console.log(res,'结果');
      let tem = { ...res.data.list };
      // console.log(tem);
      // 数据筛选 筛选未绑定数据
      for (let key in tem) {
        // console.log(key,'key');
        // console.log(this.deviceData,'ddd');
        if (tem[key].engineer_id == null) {
          this.deviceData.push(tem[key]);
        }
      }
      console.log(this.deviceData,'ddd');
    },
    handleClose(done) {
      if (this.loading) {
        return;
      }
      this.$confirm("确定要提交表单吗？")
        .then((_) => {
          this.loading = true;
          this.timer = setTimeout(() => {
            done();
            // 动画关闭需要一定的时间
            setTimeout(() => {
              this.loading = false;
            }, 400);
          }, 2000);
        })
        .catch((_) => {});
    },
    cancelForm() {
      this.loading = false;
      this.dialog = false;
      clearTimeout(this.timer);
    },
  },
  created() {
    // 路由接收工程信息参数

    // console.log(this.$route.query);
    this.engineerData = this.$route.query;
    this.getDeviceData();
    
  },
  mounted() {},
};
</script>
<style scoped>
</style>
<style>
.content1 .el-col {
  margin: 12px 0px;
  font-weight: 700;
  padding-left: 40px;
}
.content1 .el-col .bg-purple-light {
  font-weight: 400;
}

.content1 {
  padding-bottom: 13px;
  border-bottom: 1px solid #f4f4f4;
}
</style>